// $codepro.audit.disable numericLiterals, lineLength, methodChainLength
/**
 * Create Account Class
 * 
 * Description:
 * 
 * @author Team Awesome Express
 * @version $ Revision 1.0 $
 */



import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.GroupLayout;

import javax.swing.JPanel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import java.awt.Color;

import javax.swing.ButtonGroup;

/**
 * Create new account for patient
 *
 * @author Kevin
 * @version $Revision: 1.0 $
 */
public class CreateAccount extends JPanel{
        /**
         * Field gender
         */
        private String gender;
        
        /**
         * Field rdbtnFemale.
         */
        private JRadioButton rdbtnFemale;
        
        /**
         * Field rdbtnMale.
         */
        private JRadioButton rdbtnMale;

        /**
         * Field signUpDataManager.
         */
        private DataManager signUpDataManager;
        
        /**
         * Field frmCreateAccount.
         */
        private JFrame frmCreateAccount;
        
        /**
         * Field txtLastName.
         */
        private JTextField txtLastName;
        
        /**
         * Field txtAge.
         */
        private JTextField txtAge;
        
        /**
         * Field txtSSN.
         */
        private JTextField txtSSN;
        
        /**
         * Field txtPhoneNumber.
         */
        private JTextField txtPhoneNumber;
        
        /**
         * Field txtAddress.
         */
        private JTextField txtAddress;
        
        /**
         * Field txtEmail.
         */
        private JTextField txtEmail;
        
        /**
         * Field txtAllergies.
         */
        private JTextField txtAllergies;
        
        /**
         * Field txtPharmacy.
         */
        private JTextField txtPharmacy;
        
        /**
         * Field txtInsurance.
         */
        private JTextField txtInsurance;
        
        /**
         * Field txtUserid.
         */
        private JTextField txtUserid;
        
        /**
         * Field txtCreatePassword.
         */
        private JTextField txtCreatePassword;
        
        /**
         * Field txtRetypePW.
         */
        private JTextField txtRetypePW;
        
        /**
         * Field txtFirstName.
         */
        private JTextField txtFirstName;
        
        /**
         * Field checkData.
         */
        private final DataIntegrity checkData;
        
        /**
         * Field isEmpty.
         */
        /**
         * Field foundErr.
         */
        private boolean foundErr, isEmpty;

        /**
         * Field buttonGroup.
         */
        private final ButtonGroup buttonGroup = new ButtonGroup();

        /**
         * Create the application.
         * @param signUpDataManager DataManager
         */
        public CreateAccount(DataManager signUpDataManager) {
        		checkData = new DataIntegrity();
                this.signUpDataManager = signUpDataManager;
                initialize();
        }

        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
                frmCreateAccount = new JFrame();
                frmCreateAccount.getContentPane().setBackground(GlobalVars.BACKGROUND);
                frmCreateAccount.setTitle("Create Account");
                frmCreateAccount.setBounds(100, 100, 550, 500); // $codepro.audit.disable numericLiterals
                frmCreateAccount.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                
                rdbtnFemale = new JRadioButton("Female");
                buttonGroup.add(rdbtnFemale);
                rdbtnMale = new JRadioButton("Male");
                buttonGroup.add(rdbtnMale);
                
                final JLabel lblUserid = new JLabel("* UserID :");
                
                final JLabel lblIDErr = new JLabel();
                lblIDErr.setForeground(Color.RED);

                final JLabel lblCreatePassword = new JLabel("* Create Password :");
                
                final JLabel lblPWErr = new JLabel();
                lblPWErr.setForeground(Color.RED);
                
                final JLabel lblRetypePassword = new JLabel("* Retype Password :");
                
                final JLabel lblPWMatchingError = new JLabel();
                lblPWMatchingError.setForeground(Color.RED);
                
                final JLabel lblFirstName = new JLabel("* First Name :");
                
                final JLabel lblNameErr = new JLabel();
                lblNameErr.setForeground(Color.RED);
                
                final JLabel lblLastName = new JLabel("* Last Name :");
                
                final JLabel lblGender = new JLabel("* Gender :");
                
                final JLabel lblAge = new JLabel("* Age :");

                final JLabel lblAgeErr = new JLabel();
                lblAgeErr.setForeground(Color.RED);
                
                txtLastName = new JTextField();
                txtLastName.setColumns(10);// $codepro.audit.disable numericLiterals
                
                txtAge = new JTextField();
                txtAge.setColumns(10); // $codepro.audit.disable numericLiterals
                
                final JLabel lblSocialSecurity = new JLabel("Social Security # :");
                
                txtSSN = new JTextField();
                txtSSN.setColumns(10); // $codepro.audit.disable numericLiterals
                
                final JLabel lblPhoneNumber = new JLabel("Phone Number :");
                
                txtPhoneNumber = new JTextField();
                txtPhoneNumber.setColumns(10); // $codepro.audit.disable numericLiterals
                
                final JLabel lblAddress = new JLabel("Address :");
                
                txtAddress = new JTextField();
                txtAddress.setColumns(10); // $codepro.audit.disable numericLiterals
                
                final JLabel lblEmailAddress = new JLabel("Email Address :");
                
                txtEmail = new JTextField();
                txtEmail.setColumns(10); // $codepro.audit.disable numericLiterals
                
                final JLabel lblAllergies = new JLabel("Allergies :");
                
                txtAllergies = new JTextField();
                txtAllergies.setColumns(10); // $codepro.audit.disable numericLiterals
                
                final JLabel lblPharmacy = new JLabel("Pharmacy :");
                
                txtPharmacy = new JTextField();
                txtPharmacy.setColumns(10); // $codepro.audit.disable numericLiterals
                
                final JLabel lblInsurance = new JLabel("Insurance :");
                
                txtInsurance = new JTextField();
                txtInsurance.setColumns(10); // $codepro.audit.disable numericLiterals
                
                final JButton btnLogin = new JButton("Save");
                btnLogin.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent arg0){
                	   lblIDErr.setVisible(false);
                       lblPWErr.setVisible(false);
                       lblPWMatchingError.setVisible(false);
                       lblNameErr.setVisible(false);
                       lblAgeErr.setVisible(false);
                	   foundErr = false;
                	   isEmpty = false;
                	   
                	   /**
                	    * userid, pw, name, gender, age should be fill out
                	    */
                        //Check whether user id exist in the database
                        final String[] userid = signUpDataManager.getDataAtCol(GlobalVars.USER_SHEET, "Username", "Username");
                        if(!txtUserid.getText().equals("")){
	                        for(int i=0; i < userid.length; i++){
	                        	if(userid[i].equals(txtUserid.getText())){
	                        		lblIDErr.setText("Duplicated ID");
	                        		lblIDErr.setVisible(true);
	                        		foundErr = true;
	                        	}
	                        }
                        }
                        else{
                        	lblIDErr.setText("Enter your desired userID");
                        	lblIDErr.setVisible(true);
                        	isEmpty = true;
                        }
                        
                        //Check password
                        if(!txtCreatePassword.getText().equals("")){
	                       	//Check password matches with retyped password
	                       	if(!txtCreatePassword.getText().equals(txtRetypePW.getText())){
	                       		lblPWMatchingError.setText("Matching Error");
	                       		lblPWMatchingError.setVisible(true);
	                       		foundErr = true;
	                       	}
                        }
                        else{
                        	lblPWErr.setText("Enter a password and re-enter it");
                        	lblPWErr.setVisible(true);
                        	isEmpty = true;
                        }
                        
                        //Check first name
                        if(!txtFirstName.getText().equals("") || !txtLastName.getText().equals("")){
                        	if(!checkData.isOnlyLetters(txtFirstName.getText()) || !checkData.isOnlyLetters(txtLastName.getText())){
                        		lblNameErr.setText("Verify entered information");
                        		lblNameErr.setVisible(true);
                        		foundErr = true;
                        	}
                        }
                        else{
                        	lblNameErr.setText("Missing required information");
                        	lblNameErr.setVisible(true);
                        	isEmpty = true;
                        }
                       
                        //Check age
                        if(!txtAge.getText().equals("")){
                        	if(!checkData.isOnlyNumbers(txtAge.getText())){
                        		lblAgeErr.setText("Varify entered information");
                        		lblAgeErr.setVisible(true);
                        		foundErr = true;
                        	}
                        }
                        else{
                        	lblAgeErr.setText("Missing required information");
                    		lblAgeErr.setVisible(true);
                        	isEmpty = true;
                        }
                        
                        /**
                         * Fill out the empty sections to "N/A"
                         * S.S#, phone#, address, email, allergies, pharmacy, insurance
                         */
                        final String blank = "N/A";
                        if(!checkData.isNotNull(txtSSN.getText())){
                        	txtSSN.setText(blank);
                        }
                        if(!checkData.isNotNull(txtPhoneNumber.getText())){
                        	txtPhoneNumber.setText(blank);
                        }
                        if(!checkData.isNotNull(txtAddress.getText())){
                        	txtAddress.setText(blank);
                        }
                        if(!checkData.isNotNull(txtEmail.getText())){
                        	txtEmail.setText(blank);
                        }
                        if(!checkData.isNotNull(txtAllergies.getText())){
                        	txtAllergies.setText(blank);
                        }
                        if(!checkData.isNotNull(txtPharmacy.getText())){
                        	txtPharmacy.setText(blank);
                        }
                        if(!checkData.isNotNull(txtInsurance.getText())){
                        	txtInsurance.setText(blank);
                        }
                        
                        /**
                         * Every data field is correct
                         */                    
                        if(!foundErr && !isEmpty){
	                        // Make it so that the radio button will pass in correct gender
                        	gender = createButtonPressed(); // Will get gender, Female by default

                            signUpDataManager.createUserData(txtUserid.getText(), txtRetypePW.getText(), "Patient", "Unlocked", "0");

                            final Person newPatient = new Patient(txtFirstName.getText(), txtLastName.getText(), gender, txtAge.getText(), txtSSN.getText(), txtPhoneNumber.getText(), txtAddress.getText(), txtEmail.getText(),
                                            txtAllergies.getText(), txtPharmacy.getText(), txtInsurance.getText(), txtUserid.getText());
   
                            signUpDataManager.setData(GlobalVars.PATIENT_SHEET, newPatient, null, -1); // Create new patient
                            frmCreateAccount.dispose();
                        }
                    }
                });
                
                final JButton btnCancel = new JButton("Cancel");
                btnCancel.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent arg0){
                                frmCreateAccount.dispose();
                        }
                });
                
                txtUserid = new JTextField();
                txtUserid.setColumns(10); // $codepro.audit.disable numericLiterals
                
                txtCreatePassword = new JTextField();
                txtCreatePassword.setColumns(10); // $codepro.audit.disable numericLiterals
                
                txtRetypePW = new JTextField(); 
                txtRetypePW.setColumns(10); // $codepro.audit.disable numericLiterals
                
                txtFirstName = new JTextField();
                txtFirstName.setColumns(10);
                
                final GroupLayout groupLayout = new GroupLayout(frmCreateAccount.getContentPane());
                groupLayout.setHorizontalGroup(
                	groupLayout.createParallelGroup(Alignment.LEADING)
                		.addGroup(groupLayout.createSequentialGroup()
                			.addGap(178)
                			.addComponent(btnLogin)
                			.addGap(18)
                			.addComponent(btnCancel)
                			.addContainerGap(216, Short.MAX_VALUE))
                		.addGroup(groupLayout.createSequentialGroup()
                			.addContainerGap()
                			.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
                				.addComponent(lblInsurance)
                				.addComponent(lblPharmacy)
                				.addComponent(lblCreatePassword)
                				.addComponent(lblUserid)
                				.addComponent(lblRetypePassword)
                				.addComponent(lblFirstName)
                				.addComponent(lblAge)
                				.addComponent(lblSocialSecurity)
                				.addComponent(lblPhoneNumber)
                				.addComponent(lblAddress)
                				.addComponent(lblEmailAddress)
                				.addComponent(lblAllergies))
                			.addPreferredGap(ComponentPlacement.UNRELATED)
                			.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                				.addGroup(groupLayout.createSequentialGroup()
                					.addPreferredGap(ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
                						.addComponent(txtEmail, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 179, GroupLayout.PREFERRED_SIZE)
                						.addGroup(groupLayout.createSequentialGroup()
                							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                								.addComponent(lblAgeErr)
                								.addGroup(groupLayout.createSequentialGroup()
                									.addComponent(txtFirstName, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)
                									.addGap(45)
                									.addComponent(lblLastName))
                								.addGroup(groupLayout.createSequentialGroup()
                									.addComponent(txtAge, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)
                									.addPreferredGap(ComponentPlacement.RELATED, 80, Short.MAX_VALUE)
                									.addComponent(lblGender))
                								.addComponent(txtSSN, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)
                								.addComponent(txtPhoneNumber, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)
                								.addComponent(txtRetypePW, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)
                								.addComponent(txtCreatePassword, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)
                								.addComponent(txtUserid, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE))
                							.addPreferredGap(ComponentPlacement.UNRELATED)
                							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                								.addGroup(groupLayout.createSequentialGroup()
                									.addComponent(rdbtnFemale)
                									.addGap(18)
                									.addComponent(rdbtnMale))
                								.addComponent(txtLastName, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)))
                						.addComponent(txtAddress, Alignment.LEADING, 325, 325, 325)
                						.addComponent(txtAllergies, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 179, GroupLayout.PREFERRED_SIZE)
                						.addComponent(txtPharmacy, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 179, GroupLayout.PREFERRED_SIZE)
                						.addComponent(txtInsurance, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 179, GroupLayout.PREFERRED_SIZE))
                					.addGap(27))
                				.addGroup(groupLayout.createSequentialGroup()
                					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                						.addComponent(lblNameErr)
                						.addComponent(lblPWMatchingError)
                						.addComponent(lblPWErr)
                						.addComponent(lblIDErr))
                					.addContainerGap(414, Short.MAX_VALUE))))
                );
                groupLayout.setVerticalGroup(
                	groupLayout.createParallelGroup(Alignment.LEADING)
                		.addGroup(groupLayout.createSequentialGroup()
                			.addContainerGap()
                			.addComponent(lblIDErr)
                			.addPreferredGap(ComponentPlacement.RELATED)
                			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                				.addComponent(lblUserid)
                				.addComponent(txtUserid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                			.addPreferredGap(ComponentPlacement.RELATED)
                			.addComponent(lblPWErr)
                			.addGap(7)
                			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                				.addComponent(lblCreatePassword)
                				.addComponent(txtCreatePassword, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                			.addPreferredGap(ComponentPlacement.RELATED)
                			.addComponent(lblPWMatchingError)
                			.addPreferredGap(ComponentPlacement.RELATED)
                			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                				.addComponent(lblRetypePassword)
                				.addComponent(txtRetypePW, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                			.addPreferredGap(ComponentPlacement.RELATED)
                			.addComponent(lblNameErr)
                			.addGap(9)
                			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                				.addComponent(lblFirstName)
                				.addComponent(txtFirstName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                				.addComponent(lblLastName)
                				.addComponent(txtLastName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                			.addGap(6)
                			.addComponent(lblAgeErr)
                			.addPreferredGap(ComponentPlacement.RELATED)
                			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                				.addComponent(txtAge, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                				.addComponent(lblAge)
                				.addComponent(rdbtnFemale)
                				.addComponent(lblGender)
                				.addComponent(rdbtnMale))
                			.addPreferredGap(ComponentPlacement.UNRELATED)
                			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                				.addComponent(lblSocialSecurity)
                				.addComponent(txtSSN, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                			.addPreferredGap(ComponentPlacement.RELATED)
                			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                				.addComponent(lblPhoneNumber)
                				.addComponent(txtPhoneNumber, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                			.addPreferredGap(ComponentPlacement.UNRELATED)
                			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                				.addComponent(lblAddress)
                				.addComponent(txtAddress, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                			.addPreferredGap(ComponentPlacement.UNRELATED)
                			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                				.addComponent(lblEmailAddress)
                				.addComponent(txtEmail, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                			.addPreferredGap(ComponentPlacement.UNRELATED)
                			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                				.addComponent(lblAllergies)
                				.addComponent(txtAllergies, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                			.addPreferredGap(ComponentPlacement.UNRELATED)
                			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                				.addComponent(lblPharmacy)
                				.addComponent(txtPharmacy, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                			.addPreferredGap(ComponentPlacement.UNRELATED)
                			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                				.addComponent(lblInsurance)
                				.addComponent(txtInsurance, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                			.addGap(18)
                			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                				.addComponent(btnLogin)
                				.addComponent(btnCancel))
                			.addGap(46))
                );
                lblIDErr.setVisible(false);
                lblPWErr.setVisible(false);
                lblPWMatchingError.setVisible(false);
                lblNameErr.setVisible(false);
                lblAgeErr.setVisible(false);
                
                frmCreateAccount.getContentPane().setLayout(groupLayout);
                frmCreateAccount.setVisible(true);
        }
        
        /**
         *  Default is Female
        
         * @return The proper Gender */
        public String createButtonPressed() {
                if (rdbtnMale.isSelected()){
                        gender = "Male";
                }
                else {
                        gender = "female";
                }
                
                return gender;
        }
}